package net.bbtang.website.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bbtang.website.mapper.UserAccountMapper;
import net.bbtang.website.mapper.UserMapper;
import net.bbtang.website.mapper.UserSMSMapper;
import net.bbtang.website.pojo.UserDTO;
import net.bbtang.website.pojo.UserSmsDTO;
import net.bbtang.website.service.UserInfoService;
import net.bbtang.website.utils.sms.AliSmsParams;
import net.bbtang.website.utils.sms.AliSmsUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyun.mns.model.TopicMessage;




@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
	
	
	@Autowired
	UserMapper userMapper;
	@Autowired 
	UserSMSMapper userSMSMapper;
	
	@Autowired
	UserAccountMapper userAccountMapper;

	@Override
	public List<UserDTO> getUserInfoById(Map<String,Object> user) {
		return userMapper.getUserInfoById(user);
	}

	@Override
	public int addUserInfo(UserDTO user) {
		int userId = userMapper.addUserInfo(user);
		user.getUserAccount().setUserId(userId);
		userAccountMapper.addUserAccount(user.getUserAccount());
		return userId;
	}

	@Override
	public void updateUserInfo(UserDTO user) {
		userMapper.updateUserInfo(user);
		if(user.getUserAccount() != null && !user.getUserAccount().getUserAccountType().equals(0)){
			userAccountMapper.updateUserAccount(user.getUserAccount());
		}
		
	}
	
	@Override
	public UserSmsDTO getPhoneCodeByNum(String telphone,String phoneNum) {
		return userSMSMapper.getPhoneByNum(telphone,phoneNum);
	}

	@Override
	public String getPhoneCode(String phone) {
		//TODO 发送验证码
		AliSmsParams params = new AliSmsParams();
		params.setAccessId("LTAIbZYdKAYfbZVr");
		params.setAccessKey("cJ31UrQfH97Sq9Yvxr6B1kJNNF8yLI");
		params.setAccountEndpoint("https://1282459339738101.mns.cn-hangzhou.aliyuncs.com/");
		params.setTopic("sms.topic-cn-hangzhou");
		params.setSignName("赵新的测试签名");
		params.setTemplateCode("SMS_62870164");
		
		params.setPhoneNum(new String[]{phone});
		Map<String,String> templateParams = new HashMap<String,String>();
		String token = createRandom(true,6);
		templateParams.put("token", token);
		params.setTemplateParams(templateParams);
		
		TopicMessage ret = AliSmsUtils.sendSms(params);
		
		userSMSMapper.saveUserSms(phone, token);
		return token;
	}
	
	/** 
	  * 创建指定数量的随机字符串 
	  * @param numberFlag 是否是数字 
	  * @param length 
	  * @return 
	  */  
	 public static String createRandom(boolean numberFlag, int length){  
	  String retStr = "";  
	  String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";  
	  int len = strTable.length();  
	  boolean bDone = true;  
	  do {  
	   retStr = "";  
	   int count = 0;  
	   for (int i = 0; i < length; i++) {  
	    double dblR = Math.random() * len;  
	    int intR = (int) Math.floor(dblR);  
	    char c = strTable.charAt(intR);  
	    if (('0' <= c) && (c <= '9')) {  
	     count++;  
	    }  
	    retStr += strTable.charAt(intR);  
	   }  
	   if (count >= 2) {  
	    bDone = false;  
	   }  
	  } while (bDone);  
	  
	  return retStr;  
	 }


}
